Page({
  /**
   * 页面的初始数据
   */
  data: {
    city:"",
 todaytemp:"",
 todaytxt:"",
 todaydir:"",
 todaysc:"",


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  loadInfo:function(){
   var page=this;
 
wx.getLocation({  //获取当前地址
      type: 'wgs84',
      success: function (res){
        var latitude = res.latitude // 纬度，浮点数，范围为90 ~ -90
        var longitude = res.longitude // 经度，浮点数，范围为180 ~ 
        console.log(latitude,longitude);
        page.loadCity(latitude,longitude);
      }
    })  
  },
  loadCity:function(latitude,longitude){
    var page=this;
    wx.request({
      url:'https://apis.map.qq.com/ws/geocoder/v1/?location='+latitude+','+longitude+'&key=V52BZ-3EJCD-E5L47-HUQMR-WY6PZ-5YBCJ',
   
    success: function(res) {
      console.log(res.data.result);
      var city = res.data.result.address_component.city;
      city=city.replace("市","");
      page.setData({city:city});
      page.loadWeather(city);
   
    }
    })

  },
  loadWeather:function(city){
    var page=this;
    wx.request({
      url:"https://free-api.heweather.net/s6/weather/now?location="+city+"&key=092240365ca446e6bdf8a29b4148ae8d",   
        success: function(res) {
              console.log(res.data.HeWeather6[0])           
              var tmp = res.data.HeWeather6[0].now.tmp;
              var txt = res.data.HeWeather6[0].now.cond_txt;
              var dir = res.data.HeWeather6[0].now.wind_dir;
              var sc = res.data.HeWeather6[0].now.wind_sc;
             page.setData({todaytmp:tmp,todaytxt:txt,todaydir:dir,todaysc:sc,})
            
        }
        });
  }
})